package com.isoftstone.common.base;

import com.isoftstone.common.entity.SysDict;
import com.isoftstone.common.service.ISysDictService;
import com.isoftstone.plugin.freemark.FreemarkUtils;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by admin on 16/5/11.
 */

@Component
public class GetDict implements TemplateMethodModelEx {

    @Autowired
    ISysDictService sysDictService;

    public Object exec(List list) throws TemplateModelException {

        List<SysDict> sysDictList = null;

         SysDict  sysDict = null;

        /**
         * 作者:wangjs
         * 日期:16/5/20 19:35
         * 功能: 参数类型查询
         * @param type
         */

        if (list.size() == 1)
        {
            TemplateModel templateModel = (TemplateModel) list.get(0);

            String type = FreemarkUtils.converString(templateModel);


            try
            {
                sysDictList  = sysDictService.getDictBytype(type);
            } catch (Exception e)
            {
                e.printStackTrace();
            }

            return sysDictList;
        }


        /**
         * 作者:wangjs
         * 日期:16/5/20 19:55
         * 功能: 根据类型和code查询字典
         * @param type  类型
         * @param code  编码
         *
         */

        if (list.size() == 2)
        {
            TemplateModel typeModel = (TemplateModel) list.get(0);

            String type = FreemarkUtils.converString(typeModel);

            TemplateModel codeModel = (TemplateModel) list.get(1);

            String code = FreemarkUtils.converString(codeModel);


            try
            {
                sysDict = sysDictService.getDictBytypeAndCode(type, code);
            } catch (Exception e)
            {
                e.printStackTrace();
            }

            return sysDict;
        }

        return null;

    }




}
